<?php

/*
 * This class is used for common same function.
 */

class Model_Deals extends Model_Base {
	
	protected $_name,$_primaryKey;
    
    public function init() {
        $this->_name = $this->DEALS_TBL;
        $this->_primaryKey = 'deal_id';
    }
	
	 
	public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
	
	public function getDealcurrencyArray() {
        return array(
            "" => "-- Select Deal Currency --",
            "us_dollar" => "US Dollar",
            "euro" => "Euro"
        );
    }
	
	public function getDealtypeArray() {
        return array(
            "" => "",
            "sales" => "Sales",
            "integrated_sales" => "Integrated sales",
			"merchandise_sales" => "Merchandise sales",
            "services" => "Services",
			"after_sales_service" => "After sales service"
        );
    }
	
	public function getDealstageArray() {
        return array(
            "" => "",
            "in_development" => "In Development",
            "waiting_for_details" => "Waiting for Details",
			"proposal/price_quote" => "Proposal/Price Quote",
            "negotiation/review" => "Negotiation/Review",
			"closed_won" => "Closed Won",
            "on_hold" => "On Hold",
			"closed_lost" => "Closed Lost"
        );
    }
	
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('deals' => $this->DEALS_TBL))
                ->where('deal_id = ?', $id)
				->where("deal_status != 2");
     
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	 public function fetchDealData($where = "", $limit = "", $offset = "0", $order = "deal_id", $sort = "ASC") {
       $select = $this->select()
                ->from(array("deals" => $this->DEALS_TBL))
				->setIntegrityCheck(false)
				->join(array('lgn' => 'tbl_login'), "deals.deal_responsible = lgn.lgn_id")
				->where("deal_status != 2");
        if ($where != "") {
            $select->where($where);
        }
		//echo $select;exit;
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	public function  fetchDeal($where = ""){
        
        $select = $this->select()->setIntegrityCheck(false)
                ->from(array("deals" => $this->DEALS_TBL))
                ->where("deal_status != 2");
		if ($where != '') {
            $select->where($where);
        }
		
        $result = $this->fetchAll($select);
		//echo $select;
		
        $finalArray[''] = '';
        foreach ($result as $res) {
            
            $finalArray[$res['deal_id']] = $res['deal_name'];
        }
		
        return $finalArray;
		
    }
	
	public function getDealCount($where = "") {
        $select = $this->select()
                ->from(array("deals" => $this->DEALS_TBL), Array('cnt' => 'count(deal_id)'))
			    ->where("deal_status != 2");
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
	
	public function deleteDeal($dealArray) {
        if (is_array($dealArray)) {
            foreach ($dealArray as $_deal) {
                $dealData = Array();
                $dealData = $this->fetchEntryById($_deal);
                if (count($dealData) > 0) {
                    $data = array("deal_status" => "2");
                    $this->update($data, "deal_id =" . (int) $_deal);
                }
            }
        } else {
            $deal_id = $dealArray;
            $dealData = $this->fetchEntryById($deal_id);
            if (count($dealData) > 0) {
                $data = array("deal_status" => "2");
                $this->update($data, "deal_id =" . $deal_id);
            }
        }
    }
}